package com.bocai.controller;

import com.bocai.bean.SysUserInfo;
import com.bocai.bean.TUserCode;
import com.bocai.bean.TUserInfo;
import com.bocai.service.IUserService;
import com.bocai.session.BaseSession;
import com.bocai.utils.*;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by tu_pei on 2018/4/9.
 */
@Controller
@RequestMapping("user")
public class UserController extends BaseSession {
    @Autowired
    private IUserService userService;

    /** bocai-web/controller
     *
    @ApiOperation(value = "注册用户", notes = "用户注册")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "userPhone", dataType = "string", required = true, value = "手机号", defaultValue = "182555655996"),
            @ApiImplicitParam(paramType = "query", name = "userName", dataType = "string", required = false, value = "昵称", defaultValue = "豆豆"),
            @ApiImplicitParam(paramType = "query", name = "passWord", dataType = "string", required = true, value = "密码", defaultValue = "123456"),
            @ApiImplicitParam(paramType = "query", name = "salesId", dataType = "int", required = false, value = "分销商id", defaultValue = "1"),
            @ApiImplicitParam(paramType = "query", name = "userCode", dataType = "string", required = true, value = "验证码", defaultValue = "123456")
    })
    @RequestMapping(value = "/register", method = RequestMethod.GET)
    @ResponseBody
    public ResponseData registerUser(String userPhone, String userName, String passWord, Integer salesId, String userCode) {

        ResponseData data = new ResponseData();
        CodeEnum codeEnum = ObjUtils.validateParams(new Object[]{userPhone, userCode});
        if (ObjUtils.isNotEmpty(codeEnum)) {
            data.setCodeEnum(codeEnum);
            return data;
        }
        TUserCode tUserCode = userService.selectUserCode(userPhone);
        if (!userCode.equals(tUserCode.getUserCode())) {
            data.setCodeEnum(CodeEnum.CODE_10001);
            return data;
        }
        passWord = MD5Utils.md5Password(passWord);
        //注册playerId
        Map<String, Object> business = new HashMap<>();
        business.put("correlationPlayerId", userPhone);
        business.put("nickName", userName);
        JSONObject userObject = GetResultUtils.getJsonResult(SysConstant.REGISTER, business);
        String playerId = userObject.getJSONObject("body").getJSONObject("data").getString("playerId");
        //注册accountId
        Map<String, Object> business2 = new HashMap<>();
        business2.put("playerId", playerId);
        JSONObject userAccountId = GetResultUtils.getJsonResult(SysConstant.CREATE_ACCOUNT, business2);
        String accountId = userAccountId.getJSONObject("body").getJSONObject("data").getString("accountId");
        //插入数据库
        TUserInfo userInfo = new TUserInfo();
        userInfo.setSalesId(salesId);
        userInfo.setUserPhone(userPhone);
        userInfo.setUserName(userName);
        userInfo.setPassword(passWord);
        userInfo.setUserPlayerId(playerId);
        userInfo.setUserAccountId(accountId);
        data.setResults(userService.insertSelective(userInfo));
        return data;
    }

    @ApiOperation(value = "客户端修改用户信息", notes = "修改用户信息")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "userId", dataType = "integer", value = "用户Id 比传", defaultValue = ""),
            @ApiImplicitParam(paramType = "query", name = "userName", dataType = "string", value = "firstname", defaultValue = ""),
            @ApiImplicitParam(paramType = "query", name = "userIdNumber", dataType = "string", value = "id", defaultValue = ""),
            @ApiImplicitParam(paramType = "query", name = "userEMail", dataType = "string", value = "邮箱", defaultValue = ""),
            @ApiImplicitParam(paramType = "query", name = "userAddress1", dataType = "string", value = "地址1", defaultValue = ""),
            @ApiImplicitParam(paramType = "query", name = "userAddress2", dataType = "string", value = "地址2", defaultValue = ""),
            @ApiImplicitParam(paramType = "query", name = "userCity", dataType = "string", value = "城市", defaultValue = ""),
            @ApiImplicitParam(paramType = "query", name = "userProvince", dataType = "string", value = "省份", defaultValue = ""),
            @ApiImplicitParam(paramType = "query", name = "sex", dataType = "integer", value = "性别，1男，0女", defaultValue = ""),
            @ApiImplicitParam(paramType = "query", name = "surName", dataType = "string", value = "surname", defaultValue = ""),
    })
    @RequestMapping(value = "/updateUserInfos", method = RequestMethod.GET)
    @ResponseBody
    public ResponseData updateUserInfos(Integer userId,
                                        String userName,
                                        String userIdNumber,
                                        String userEMail,
                                        String userAddress1,
                                        String userAddress2,
                                        String userCity,
                                        String userProvince,
                                        Integer sex,
                                        String surName) {
        ResponseData data = new ResponseData();

        if (null == userId || userId <= 0) {
            data.setCodeEnum(CodeEnum.CODE_90002);
        } else {
            TUserInfo userInfo = new TUserInfo();
            userInfo.setUserId(userId);
            userInfo.setUserName(userName);
            userInfo.setUserIdNumber(userIdNumber);
            userInfo.setUserEMail(userEMail);
            userInfo.setUserAddress1(userAddress1);
            userInfo.setUserAddress2(userAddress2);
            userInfo.setUserCity(userCity);
            userInfo.setUserProvince(userProvince);
            userInfo.setSex(sex);
            userInfo.setSurName(surName);
            if (userService.updateByPrimaryKeySelective(userInfo) > 0) {
                data.setCodeEnum(CodeEnum.SUCCESS);
            } else {
                data.setCodeEnum(CodeEnum.CODE_1);
            }
        }
        return data;
    }
*/

    @ApiOperation(value = "查询用户信息", notes = "查询用户信息")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "userId", dataType = "integer", value = "用户Id", defaultValue = "1"),
    })
    @RequestMapping(value = "/findUserInfo", method = RequestMethod.GET)
    @ResponseBody
    public ResponseData findUserInfo(Integer userId) {
        ResponseData data = new ResponseData();
        if (null == userId || userId <= 0) {
            data.setCodeEnum(CodeEnum.CODE_90002);
        } else {
            data.setResults(userService.selectByPrimaryKey(userId));
        }
        return data;
    }

    @ApiOperation(value = "查询用户列表", notes = "查询用户列表")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "accountNumberId", dataType = "integer", value = "后台用户Id", defaultValue = "1"),
    })
    @RequestMapping(value = "/findUserInfoList", method = RequestMethod.POST)
    @ResponseBody
    public ResponseData findUserInfoList(Integer accountNumberId, String phone) {
        ResponseData data = new ResponseData();
        return userService.selectUserInfoAll(accountNumberId, phone, data);
    }

    @ApiOperation(value = "禁用或者启用用户", notes = "禁用或者启用用户")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "accountNumberId", dataType = "integer", value = "后台用户Id", defaultValue = "1"),
            @ApiImplicitParam(paramType = "query", name = "userId", dataType = "integer", value = "用户Id", defaultValue = "1"),
            @ApiImplicitParam(paramType = "query", name = "userStatus", dataType = "integer", value = "启用 1  禁用 0", defaultValue = "1"),
    })
    @RequestMapping(value = "/updateUserInfo", method = RequestMethod.GET)
    @ResponseBody
    public ResponseData updateUserInfo(Integer userId, Integer userStatus, Integer accountNumberId) {
        ResponseData data = new ResponseData();
        if (null == userId || userId <= 0) {
            data.setCodeEnum(CodeEnum.CODE_90002);
        }
        return userService.updateUserInfo(userId, userStatus, accountNumberId, data);
    }
/** bocai-web/controller
 *
    @ApiOperation(value = "前端登陆", notes = "前端登陆")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "accountNumber", dataType = "string", value = "账号", defaultValue = "1"),
            @ApiImplicitParam(paramType = "query", name = "password", dataType = "string", value = "密码", defaultValue = "1"),
    })
    @RequestMapping(value = "/sign", method = RequestMethod.GET)
    @ResponseBody
    public ResponseData signIn(String accountNumber, String password) {
        ResponseData data = new ResponseData();
        if (null == accountNumber || accountNumber.trim().length() <= 0 || null == password || password.trim().length() <= 0) {
            data.setCodeEnum(CodeEnum.CODE_90002);
        } else {
            TUserInfo userInfo = userService.selectUserInfo(accountNumber);
            if (null == userInfo || null == userInfo.getPassword() || userInfo.getPassword().trim().length() <= 0) {
                data.setCodeEnum(CodeEnum.CODE_10003);
            } else {
                password = MD5Utils.md5Password(password);
                if (userInfo.getPassword().equals(password) && 1 == userInfo.getUserStatus()) {
                    data.setCodeEnum(CodeEnum.SUCCESS);
                    data.setResults(userInfo);
                } else if (!userInfo.getPassword().equals(password)) {
                    data.setCodeEnum(CodeEnum.CODE_90004);
                } else {
                    data.setCodeEnum(CodeEnum.CODE_90005);
                }
            }
        }
        return data;
    }
*/

    @ApiOperation(value = "后台登陆", notes = "后台登陆")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "accountNumber", dataType = "string", value = "账号", defaultValue = "1"),
            @ApiImplicitParam(paramType = "query", name = "password", dataType = "string", value = "密码", defaultValue = "1"),
    })
    @RequestMapping(value = "/signBackstage", method = RequestMethod.POST)
    @ResponseBody
    public ResponseData signInBackstage(String accountNumber, String password) {
        ResponseData data = new ResponseData();
        if (null == accountNumber || accountNumber.trim().length() <= 0 || null == password || password.trim().length() <= 0) {
            data.setCodeEnum(CodeEnum.CODE_90002);
        } else {
            SysUserInfo sysUserInfo = userService.selectSysUserInfo(accountNumber);
            if (null == sysUserInfo || null == sysUserInfo.getPassword() || sysUserInfo.getPassword().trim().length() <= 0) {
                data.setCodeEnum(CodeEnum.CODE_10003);
            } else {
                if (sysUserInfo.getPassword().equals(password)) {
                    getRequest().getSession().setAttribute("systemUserId", sysUserInfo.getUserId());
                    data.setCodeEnum(CodeEnum.SUCCESS);
                    data.setResults(sysUserInfo);
                } else {
                    data.setCodeEnum(CodeEnum.CODE_90004);
                }
            }
        }
        return data;
    }


    @ApiOperation(value = "后台用户重置用户密码", notes = "重置用户密码")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "userId", dataType = "integer", value = "用户Id", defaultValue = "1"),
    })
    @RequestMapping(value = "/updatePassword", method = RequestMethod.GET)
    @ResponseBody
    public ResponseData updatePassword(Integer userId) {
        ResponseData data = new ResponseData();
        if (null == userId || userId <= 0) {
            data.setCodeEnum(CodeEnum.CODE_90002);
        }
        return userService.updatePassword(userId, data);
    }

    @ApiOperation(value = "修改密码", notes = "用户修改密码")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "userId", dataType = "int", required = true, value = "用户id", defaultValue = "1"),
            @ApiImplicitParam(paramType = "query", name = "oldPassWord", dataType = "string", required = true, value = "用户旧密码", defaultValue = "123456"),
            @ApiImplicitParam(paramType = "query", name = "newPassWord", dataType = "string", required = true, value = "用户新密码", defaultValue = "654321"),
    })
    @RequestMapping(value = "/updatePwd", method = RequestMethod.GET)
    @ResponseBody
    public ResponseData updatePwd(int userId, String oldPassWord, String newPassWord) {

        ResponseData data = new ResponseData();
        CodeEnum codeEnum = ObjUtils.validateParams(new Object[]{userId, oldPassWord, newPassWord});
        if (ObjUtils.isNotEmpty(codeEnum)) {
            data.setCodeEnum(CodeEnum.CODE_90001);
            return data;
        }
        String oldPwd = MD5Utils.md5Password(oldPassWord);
        String newPwd = MD5Utils.md5Password(newPassWord);
        String nowPwd = userService.selectByPrimaryKey(userId).getPassword();
        if (oldPwd.equals(nowPwd)) {
            TUserInfo userInfo = new TUserInfo();
            userInfo.setUserId(userId);
            userInfo.setPassword(newPwd);
            if (userService.updateByPrimaryKeySelective(userInfo) > 0) {
                data.setCodeEnum(CodeEnum.SUCCESS);
            }
        } else {
            data.setCodeEnum(CodeEnum.CODE_10002);
        }
        return data;
    }

    @ApiOperation(value = "忘记密码", notes = "用户寻找密码")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "userId", dataType = "int", required = true, value = "用户id", defaultValue = "1"),
            @ApiImplicitParam(paramType = "query", name = "newPassWord", dataType = "string", required = true, value = "用户新密码", defaultValue = "654321"),
            @ApiImplicitParam(paramType = "query", name = "userCode", dataType = "string", required = true, value = "验证码", defaultValue = "654321"),
    })
    @RequestMapping(value = "/forgetPwd", method = RequestMethod.GET)
    @ResponseBody
    public ResponseData forgetPwd(int userId, String newPassWord, String userCode) {

        ResponseData data = new ResponseData();
        CodeEnum codeEnum = ObjUtils.validateParams(new Object[]{userId, newPassWord});
        if (ObjUtils.isNotEmpty(codeEnum)) {
            data.setCodeEnum(CodeEnum.CODE_90001);
            return data;
        }
        String newPwd = MD5Utils.md5Password(newPassWord);
        String code = userService.selectByPrimaryKey(userId).getUserCode();
        if (userCode.equals(code)) {
            TUserInfo userInfo = new TUserInfo();
            userInfo.setUserId(userId);
            userInfo.setPassword(newPwd);
            if (userService.updateByPrimaryKeySelective(userInfo) > 0) {
                data.setCodeEnum(CodeEnum.SUCCESS);
            }
        } else {
            data.setCodeEnum(CodeEnum.CODE_10002);
        }
        return data;
    }
/** bocai-web/controller
 *
    @ApiOperation(value = "获取验证码", notes = "获取验证码")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "userPhone", dataType = "string", required = true, value = "手机号", defaultValue = "182555655996"),
    })
    @RequestMapping(value = "/getPhoneCode", method = RequestMethod.GET)
    @ResponseBody
    public ResponseData registerUser(String userPhone) {
        ResponseData data = new ResponseData();
        CodeEnum codeEnum = ObjUtils.validateParams(new Object[]{userPhone});
        if (ObjUtils.isNotEmpty(codeEnum)) {
            data.setCodeEnum(codeEnum);
            return data;
        }
        int userCode = (int) ((Math.random() * 9 + 1) * 100000);
        TUserCode tUserCode = new TUserCode();
        tUserCode.setUserCode(userCode + "");
        tUserCode.setUserPhone(userPhone);
        TUserCode phoneCode = userService.insertCode(tUserCode);
        if (phoneCode != null) {
            data.setResults(phoneCode);
            return data;
        } else {
            data.setCodeEnum(CodeEnum.CODE_1);
        }
        return data;
    }
    */
}
